package com.icss.ah.ProductOffLoad.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.util.LogUtils;

@Service
@Transactional
public class ProductOffLoadService {
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	/**
	 * 已购商品列表查询
	 */
	public EasyuiDataGridJson queryProductInfoList(Map<String, String> map){
		EasyuiDataGridJson dataGridJson = new EasyuiDataGridJson();
		try {
			List<Map<String, Object>> rows = sqlSessionTemplate.selectList("ProductOffLoadBean.queryProductInfoList",map);
			Long total = sqlSessionTemplate.selectOne("ProductOffLoadBean.queryProductInfoListCount",map);
			dataGridJson.setRows(rows);
			dataGridJson.setTotal(total);
			LogUtils.info("ProductOffLoadService|queryProductInfoList");
		} catch (Exception e) {
			e.printStackTrace();
			LogUtils.error("ProductOffLoadService|queryProductInfoList",e);
		}
		return dataGridJson;
	}
	/**
	 * 保存挑选商品信息
	 * @param map
	 * @return
	 */
	public String saveSearchProductInfo(HashMap map){
		String result = "0, ";
		try {
			String uuids = map.get("uuids").toString();
			String[] productId = uuids.split(",");
			for(int i =0 ; i<productId.length;i++){
				String productInfo = productId[i];
				String[] product = productInfo.split("#@");
				map.put("oldShopId", product[0]);
				map.put("productId", product[1]);
				map.put("newProductId", UUID.randomUUID().toString());
				LogUtils.info("productOffLoadService|saveSearchProductInfo|map:"+map);
				sqlSessionTemplate.update("ProductOffLoadBean.saveSearchProductInfo", map);
			}
			result = "1,保存成功";
		} catch (Exception e) {
			LogUtils.error(e);
			result = "2,保存失败："+e.getMessage();
		}
		return result;
	}
	
	
	public List getProductType1List(String modelKey,HashMap map) {
		List list = new ArrayList();
		try {
			list = sqlSessionTemplate.selectList(modelKey,map);
			list = list == null ? new ArrayList() : list;
		} catch (Exception ex) {
			ex.printStackTrace();
			list = new ArrayList();
		} 
		return list;
	}
	
	public List getProductType2List(String modelKey,HashMap map) {
		List list = new ArrayList();
		try {
			list = sqlSessionTemplate.selectList(modelKey,map);
			list = list == null ? new ArrayList() : list;
		} catch (Exception ex) {
			ex.printStackTrace();
			list = new ArrayList();
		} 
		return list;
	}
	
	
	public List getProductList(String modelKey,HashMap map) {
		List list = new ArrayList();
		try {
			list = sqlSessionTemplate.selectList(modelKey,map);
			list = list == null ? new ArrayList() : list;
		} catch (Exception ex) {
			ex.printStackTrace();
			list = new ArrayList();
		} 
		return list;
	}
	
	public Long getProductCount(String modelKey,HashMap map) {
		Long reValue = 0l;
		try {
			reValue = (Long)sqlSessionTemplate.selectOne(modelKey,map);
		} catch (Exception ex) {
			ex.printStackTrace();
		} 
		return reValue;
	}
	
	public void productUp(String modelKey,HashMap map) {
		try {
			sqlSessionTemplate.update(modelKey,map);
			
		} catch (Exception ex) {
			ex.printStackTrace();
			
		} 
	}
	
	public void productDel(String modelKey,HashMap map) {
		try {
			sqlSessionTemplate.update(modelKey,map);
			
		} catch (Exception ex) {
			ex.printStackTrace();
			
		} 
	}
	
	
}
